﻿using SaveSation.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SaveSation
{
    public partial class GroupsForm : Form
    {
        List<GroupEntity> _groups;
        bool _changed = false;
        public GroupsForm()
        {
            InitializeComponent();
        }
        public static DialogResult Show(ref List<GroupEntity> groups)
        {
            using (var form = new GroupsForm())
            {
                form._groups = groups;
                foreach (var group in groups)
                {
                    form.comboBox1.Items.Add(group.Name);
                }
                if (groups.Count > 0) form.comboBox1.SelectedIndex = 0;
                form.ShowDialog();
                return form._changed ? DialogResult.OK : DialogResult.Cancel;
            }
        }
        private void btnAplay_Click(object sender, EventArgs e)
        {
            int i = comboBox1.SelectedIndex;
            if (i < 0) return;
            _groups[i].UUID = textBox1.Text;
            _groups[i].Name = textBox2.Text;
            _groups[i].SortNumber = int.Parse(textBox3.Text);
            comboBox1.Items.RemoveAt(i);
            comboBox1.Items.Insert(i, _groups[i].Name);
            comboBox1.SelectedIndex = i;
            _changed = true;
            //this.DialogResult = DialogResult.OK;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = comboBox1.SelectedIndex;
            textBox1.Text = _groups[i].UUID;
            textBox2.Text = _groups[i].Name;
            textBox3.Text = _groups[i].SortNumber.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GroupEntity group = new GroupEntity() { Name = $"分组 {comboBox1.Items.Count + 1}", Index = StartForm._defaultGroup.Index + _groups.Count + 1 };
            textBox1.Text = group.UUID;
            textBox2.Text = group.Name;
            textBox3.Text = group.SortNumber.ToString();

            _groups.Add(group);
            comboBox1.Items.Add(group.Name);
            comboBox1.SelectedIndex = comboBox1.Items.Count - 1;
            textBox2.Focus();
            textBox2.SelectionLength = textBox2.TextLength;
            _changed = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = comboBox1.SelectedIndex;
            if (i < 0) return;
            if (MessageBox.Show(this, $"您确认要删除分组[{_groups[i].Name}]吗？", "询问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                comboBox1.Items.RemoveAt(i);
                _groups.RemoveAt(i);
                if (comboBox1.Items.Count > 0) comboBox1.SelectedIndex = (i - 1) < 0 ? 0 : i - 1;
                _changed = true;
            }
        }
    }
}
